﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MSNPSharp;

namespace SBPweb.MediaCenter.Modules.Messenger.MSN.WF
{
    public partial class SignInForm : Form
    {
        private MSNPSharp.Messenger mMessenger;
        
        public delegate void ParameterlessHandler();
        public delegate void CursorParameteredHandler(Cursor value);

        public SignInForm()
        {
            InitializeComponent();
        }

        public SignInForm(MSNPSharp.Messenger messenger)
		{
			InitializeComponent();
			mMessenger = messenger;

			if (messenger.Owner.DisplayImage != null)
			{
				this.pbAvatar.Image = messenger.Owner.DisplayImage.Image;
			}

			mMessenger.Nameserver.SignedIn += new EventHandler(Nameserver_SignedIn);
			mMessenger.Nameserver.ServerErrorReceived += new MSNPSharp.Core.ErrorReceivedEventHandler(Nameserver_ServerErrorReceived);
		}

        public MSNPSharp.Messenger Messenger
        {
            get
            {
                return mMessenger;
            }
            set
            {
                mMessenger = value;
            }
        }

        private void SetCursor(Cursor newCursor)
        {
            if (InvokeRequired)
            {
                this.Invoke(new CursorParameteredHandler(this.SetCursor), newCursor);
                return;
            }

            this.Cursor = newCursor;
        }

		void Nameserver_ServerErrorReceived(object sender, MSNErrorEventArgs e)
		{
            this.SetCursor(Cursors.Default);
			MessageBox.Show("Connection error: {0}", e.MSNError.ToString());
		}

		void Nameserver_SignedIn(object sender, EventArgs e)
		{
			if (InvokeRequired)
			{
				this.Invoke(new ParameterlessHandler(this.Close));
				return;
			}

			this.pbSignIn.Style = ProgressBarStyle.Continuous;

			this.Close();
		}

		private void btnSignIn_Click(object sender, EventArgs e)
		{
			if (this.tbEmail.Text.Trim() != string.Empty && tbPassword.Text.Trim() != string.Empty)
			{
				this.pbSignIn.Style = ProgressBarStyle.Marquee;
				this.SetCursor(Cursors.WaitCursor);
				this.Enabled = false;
				this.Messenger.Credentials.ClientID = "msmsgs@msnmsgr.com";
                this.Messenger.Credentials.ClientCode = "Q1P7W2E4J9R8U3S5";
                this.Messenger.Credentials.Account = this.tbEmail.Text.Trim();
                this.Messenger.Credentials.Password = this.tbPassword.Text.Trim();

				try
				{
                    this.Messenger.Connect();
				}
				catch (MSNPSharp.MSNPSharpException exc)
				{
					MessageBox.Show(String.Format("Connection error!\n{0}", exc.Message));
				}
			}
			else
			{
				MessageBox.Show("The e-mail and/or the password is empty!");
			}
		}

		private void tbPassword_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				btnSignIn_Click(sender, new EventArgs());
			}
		}
    }
}
